package com.dommy.tab.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dommy.tab.R;
import com.dommy.tab.adapter.MsgContentFragmentAdapter;
import com.dommy.tab.adapter.MyWaterTwoAdapter;
import com.dommy.tab.pojo.ViewHolder;
import com.dommy.tab.pojo.WaterVO;
import com.dommy.tab.util.WaterUtil;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 展示页
 */
public class ContactFragment extends Fragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.lv_contact)
    public ListView lv;
    public static ArrayList<ArrayList<WaterVO>> list = new ArrayList<>();
    private MsgContentFragmentAdapter adapter;
    private List<String> names;
    private static WaterTwoAdapter list_item;
    public MyWaterTwoAdapter myWaterTwoAdapter;
    public static  int value = 0;
    public static ArrayList<WaterVO> list2  = new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initData();
        MsgFragment.FLAG =true;
    }
//  初始化
    private void init() {
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                list = WaterUtil.WATER_LIST;
                handler.sendEmptyMessageDelayed(1, 100);
            }
        });
        thread.start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                while(MsgFragment.FLAG){
                    thread.run();
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }
    public static void updata(){

    }


    public static Handler handler = new  Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    list2 = list.get(value);
                    list_item.notifyDataSetChanged();
                    break;
            }
        }
    };
    public static  Handler getWaterTwoHandler(){
        return handler;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        ButterKnife.bind(this, view);
//      设置viewPager
        adapter = new MsgContentFragmentAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        adapter.setList(names);
//      设置LIstView
        myWaterTwoAdapter = new MyWaterTwoAdapter();
        list_item = new WaterTwoAdapter();
        lv.setAdapter(list_item);
        return view;
    }

    private void initData() {
        names = new ArrayList<>();
        names.add("日");
        names.add("月");
        names.add("年");
    }
//  适配
    public class WaterTwoAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list2.size();
        }
        @Override
        public Object getItem(int position) {
            return list2.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = new ViewHolder();
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.water_two,null);
                viewHolder.id = (TextView) convertView.findViewById(R.id.dateTime);
                viewHolder.waterValue = (TextView) convertView.findViewById(R.id.waterValue);
                viewHolder.waterConsumer = (TextView) convertView.findViewById(R.id.waterConsumer);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            WaterVO waterVO = list2.get(position);
            viewHolder.id.setText( waterVO.getDateTime() );
            viewHolder.waterValue.setText( waterVO.getMonthWater().toString() );
            viewHolder.waterConsumer.setText( waterVO.getMonthConsumer().toString());
            return convertView;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }


}
